import { createRouter, createWebHashHistory } from "vue-router";
// import { nextTick } from "_vue@3.2.11@vue";

const routes = [
  // 一级重定向
  {
    path: "/",
    redirect: "/home",
  },
  // 一级导航
  {
    path: "/home",
    component: () => import("../views/frist/home.vue"),
    children: [
      { path: "/home", redirect: "/home/index" },
      {
        path: "/home/index",
        name: "keepalive",
        component: () => import("../views/home/index.vue"),
        // 设置路由元信息，标记该组件是缓存组件
        meta: {
          keepalive: true,
        },
      },
      {
        path: "/home/course",
        component: () => import("../views/home/course.vue"),
        // 设置路由元信息，标记该组件是缓存组件
        meta: {
          keepalive: true,
        },
        //路由独享守卫，需要进行判断，用户登陆后才能进该界面
        beforeEnter: (to, from, next) => {
          if (localStorage.getItem("username")) {
            next();
          } else {
            next("/login");
          }
        },
      },
      {
        path: "/home/growing",
        component: () => import("../views/home/growing.vue"),
        //路由独享守卫，需要进行判断，用户登陆后才能进该界面
        beforeEnter: (to, from, next) => {
          if (localStorage.getItem("username")) {
            next();
          } else {
            next("/login");
          }
        },
      },
      {
        path: "/home/my",
        component: () => import("../views/home/my.vue"),
        // 设置路由元信息，标记该组件是缓存组件
        meta: {
          keepalive: true,
        },
        //路由独享守卫，需要进行判断，用户登陆后才能进该界面
        beforeEnter: (to, from, next) => {
          if (localStorage.getItem("username")) {
            next();
          } else {
            next("/login");
          }
        },
      },
    ],
  },
  //个人信息页面
  {
    path: "/personal",
    component: () => import("../views/frist/personal.vue"),
  },
  //设置页面
  {
    path: "/setup",
    component: () => import("../views/home/setup.vue"),
  },
  //邀请有礼页面
  {
    path: "/polite",
    component: () => import("../views/home/polite.vue"),
  },
  // 详情页
  {
    path: "/detail",
    component: () => import("../views/frist/detail.vue"),
  },
  // 钻石明细
  {
    path: "/diamond",
    component: () => import("../views/frist/diamond.vue"),
  },
  // 设备
  {
    path: "/facility",
    component: () => import("../views/frist/facility.vue"),
  },
  // 完善信息
  {
    path: "/message",
    component: () => import("../views/frist/message.vue"),
  },
  {
    path: "/growing_next",
    component: () => import("../components/growing_next.vue"),
  },
  {
    path: "/Training",
    component: () => import("../components/Training.vue"),
  },
  // 登录界面
  {
    path: "/login",
    component: () => import("../views/frist/login.vue"),
  },
  // 注册界面
  {
    path: "/registry",
    component: () => import("../views/frist/registry.vue"),
  },
  {
    path: "/:pathMatch(.*)",
    component: () => import("../views/frist/nofind.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior() {
    // 始终滚动到顶部
    return { top: 0 };
  },
});

export default router;
